/**
 * Created by Churcy on 2017/1/10.
 */
/**
 * Created by Churcy on 2017/1/9.
 */

$(function() {
	Carry.searchCarry();
});

var Carry = {

	searchCarry : function() {
		$("#searchBTN").on('click', function() {
			var conditions = getCurrentConditions();
			conditions.pageNo = 1;
			ajaxSearchCarryList(conditions);
		});
		$("#resetBTN").on('click', function() {
			resetFn();
		});
	}
}
function searchFn() {
	var conditions = getCurrentConditions();
	console.log("searchFN")
	var response = ajaxSearchCarryList(conditions);
}

function resetFn(){
	var carryCode = $("#carryCode").val("");
	var lineCode = $("#lineCode").val("");
	var lineName = $("#lineName").val("");
	var deptName = $("#deptName").val("");
}

function getCurrentConditions() {
	console.log("getCurrentConditions");

	var carryCode = $("#carryCode").val();
	var lineCode = $("#lineCode").val();
	var lineName = $("#lineName").val();
	var deptName = $("#deptName").val();
	var pageNo = $("#p").val();

	var conditions = {
		carryCode : carryCode,
		lineCode : lineCode,
		lineName : encodeURI(lineName),
		deptName : encodeURI(deptName),
		pageIndex : pageNo
	};
	return conditions;

}
function ajaxSearchCarryList(conditions) {
	console.log("ajaxSearchHospitalList");
	$.ajax({
		type : "GET",
		url : PROJECT_CTX + "/trafficback/carryList",
		data : conditions,
		dataType : "json",
		success : function(data) {
			console.log(data)
			var result = eval(data);
			var resultCode = result.status.code;
			console.log(resultCode)
			console.log(result.line);
			if (1 == resultCode) {
				// 清空当前数据
				var carryList = $("#carryList").empty();
				// 填写查询出来的数据
				$.each(result.line, function(i, item) {
					var tr = "<tr>";

					tr += "<td >";
					tr += item.carryCode;
					tr += "</td>"

					tr += "<td >";
					tr += item.carryName;
					tr += "</td>"

					tr += "<td >";
					tr += item.deptCode;
					tr += "</td>"

					tr += "<td >";
					tr += item.deptName;
					tr += "</td>"

					tr += "<td >";
					tr += item.companyName;
					tr += "</td>"

					tr += "<td >";
					tr += item.isProducing;
					tr += "</td>"

					tr += "<td >";
					tr += item.lineCode;
					tr += "</td>"

					tr += "<td >";
					tr += item.lineName;
					tr += "</td>"

					tr += "<td >";
					tr += item.unitPrice;
					tr += "</td>"

					/*tr += "<td >";
					tr += " <a title='修改' href='#'>修改</a>";
					tr += " <a title='删除' href='#'>删除</a>";
					tr += "</td >";*/

					tr += "</tr>";
					carryList.append(tr);
					// 更新分页栏
					reloadPage(result);
				});
			}

		}

		,
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			console.log(XMLHttpRequest.status);
			console.log(XMLHttpRequest.readyState);
			console.log(textStatus);
		}
	});
}

function formatDate(date) {
	var date = new Date(date);
	var month = date.getMonth() + 1;
	if (month < 10) {
		month = "0" + month;
	}
	var today = date.getDate();
	if (today < 10) {
		today = "0" + today;
	}
	return date.getFullYear() + "-" + month + "-" + today;
}